package ee.nutilovi.praktikaportaal.admin;

import java.io.IOException;
import java.util.Date;

import javax.servlet.http.*;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Text;
import com.google.appengine.api.memcache.MemcacheService;
import com.google.appengine.api.memcache.MemcacheServiceFactory;

@SuppressWarnings("serial")
public class PostNewsServlet extends HttpServlet {
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

		String title = req.getParameter("title");
		Text body = new Text(req.getParameter("body"));
		Date date = new Date();
		
		Entity n = new Entity("News");
		n.setProperty("date", date);
		n.setProperty("title", title);
		n.setProperty("body", body);

		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		datastore.put(n);
		
		// Refresh stale cache.
		MemcacheService mc = MemcacheServiceFactory.getMemcacheService();
		mc.delete("news");

		resp.sendRedirect("/");
	}
}
